package com.codinghero.tiyutupian.spider;

import java.util.ArrayList;
import java.util.List;

import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.ImageTag;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import com.codinghero.tiyutupian.spider.dto.HuabanPage;
import com.codinghero.util.NumberUtils;
import com.codinghero.util.StringUtils;
import com.sina.sae.fetchurl.SaeFetchurl;

public class HuabanSpider {
	
	public List<HuabanPage> execute(String url) {
		List<HuabanPage> pages = new ArrayList<HuabanPage>();
		int fetchTimes = 0;
		while (!StringUtils.isEmpty(url) && fetchTimes < 1) {
			String content = getContent(url);
			HuabanPage page = new HuabanPage();
			page.setMidImgUrl(this.getMidImgUrl(content));
			page.calOtherImgUrl();
			pages.add(page);
			++fetchTimes;
			
			url = this.getNextUrl(content);
			if (!StringUtils.isEmpty(url)) {
				if (url.startsWith("/")) {
					url = "huaban.com" + url;
				} else {
					continue;
				}
			} else {
				break;
			}
			try {
				int millis = NumberUtils.randomInt(1000, 10000);
				Thread.sleep(millis);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		return pages;
	}
	
	private String getContent(String url) {
		return new SaeFetchurl().fetch(url);
	}
	
	private String getMidImgUrl(String content) {
		try {
			Parser parser = Parser.createParser(content, "UTF-8");
			NodeFilter imgTagFilter = new TagNameFilter("img");
			NodeFilter altFilter = new HasAttributeFilter("alt", "阿色采集到美女");
			NodeFilter imgFilter = new AndFilter(imgTagFilter, altFilter);
			
			NodeList nodeList = parser.parse(imgFilter);
			if (nodeList != null && nodeList.size() > 0) {
				ImageTag imgNode = (ImageTag) nodeList.elementAt(0);
				return imgNode.getImageURL();
			} else {
				return "";
			}
		} catch (ParserException e) {
			e.printStackTrace();
			return "";
		}
	}
	
	private String getLargeImgUrl(String content) {
		try {
			Parser parser = Parser.createParser(content, "UTF-8");
			NodeFilter aTagFilter = new TagNameFilter("a");
			NodeFilter classFilter = new HasAttributeFilter("class", "jd-link");
			NodeFilter imgFilter = new AndFilter(aTagFilter, classFilter);
			
			NodeList nodeList = parser.parse(imgFilter);
			System.out.println(nodeList.asString());
			if (nodeList != null && nodeList.size() > 0) {
				LinkTag aNode = (LinkTag) nodeList.elementAt(0);
				return aNode.getLink();
			} else {
				return "";
			}
		} catch (ParserException e) {
			e.printStackTrace();
			return "";
		}
	}
	
	private String getNextUrl(String content) {
		try {
			Parser parser = Parser.createParser(content, "UTF-8");
			NodeFilter aTagFilter = new TagNameFilter("a");
			NodeFilter classFilter = new HasAttributeFilter("title", "下一张");
			NodeFilter nextPageFilter = new AndFilter(aTagFilter, classFilter);
			
			NodeList nodeList = parser.parse(nextPageFilter);
			if (nodeList != null && nodeList.size() > 0) {
				LinkTag nextPageNode = (LinkTag) nodeList.elementAt(0);
				return nextPageNode.getLink();
			} else {
				return "";
			}
		} catch (ParserException e) {
			e.printStackTrace();
			return "";
		}
	}
}
